package com.djzhou.gmms.announcement.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.dao.announcement.AnnouncementDAO;
import com.djzhou.gmms.dao.base.impl.BaseDAOImpl;
import com.djzhou.gmms.model.announcement.TbAnnouncement;
import com.djzhou.gmms.model.company.TbCompany;
import com.djzhou.util.DateUtil;

public class AnnouncementAction extends BaseAction<TbAnnouncement> {

	private TbAnnouncement announcement;
	private List<TbAnnouncement> list;
	private Integer announcementId;
	
	@Override
	public String delete() {
		announcementService.delete(announcementId);
		msg="数据删除成功";
		setCurrentPage(currentPage);
		logService.addLog("系统公告",queryUser(),"删除",announcementId,"TbAnnouncement",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		if(announcement.getStartDate()==null)announcement.setStartDate(new Date());
		if(announcement.getEndDate()==null)announcement.setEndDate(DateUtil.turnStringToDate("2029-12-21", "yyyy-MM-dd"));
		announcementService.update(announcement);
		setCurrentPage(currentPage);
		logService.addLog("系统公告",queryUser(),"编辑",announcement.getAnnouncementId(),"TbAnnouncement",super.queryCompany(),super.queryDepartment());
		
		msg="数据保存成功";//普通页面用ajax返回信息
		
		//EXT表单AJAX提交返回用实体
		ajaxMsg=new ExtMessage();
		ajaxMsg.setSuccess(true);
		ajaxMsg.setData("数据保存成功");
		return SUCCESS;
	}

	@Override
	public String queryList() {
		if(loadData==null)return SUCCESS;
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		pageBean=announcementService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = announcementService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbAnnouncement>();
		}
		totalPage=pageBean.getTotalPage();
		
		return SUCCESS;
	}
	@Override
	public String queryList4ExtJs() {
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		pageBean=announcementService.queryPageBean(hashMap, limit, page);
		return SUCCESS;
	}
	

	@Override
	public String save() {
		if(announcement!=null){
			announcement.setDeleteFlag(0);
			if(announcement.getStartDate()==null)announcement.setStartDate(new Date());
			if(announcement.getEndDate()==null)announcement.setEndDate(DateUtil.turnStringToDate("2029-12-21", "yyyy-MM-dd"));
			announcementService.add(announcement);
		}
		setCurrentPage(currentPage);
		logService.addLog("系统公告",queryUser(),"添加",announcement.getAnnouncementId(),"TbAnnouncement",queryCompany(),super.queryDepartment());
		
		msg="数据保存成功";//普通页面用ajax返回信息
		
		//EXT表单AJAX提交返回用实体
		ajaxMsg=new ExtMessage();
		ajaxMsg.setSuccess(true);
		ajaxMsg.setData("数据保存成功");
		
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		setCurrentPage(currentPage);
		announcement=announcementService.queryById(announcementId);
		return SUCCESS;
	}

	public TbAnnouncement getAnnouncement() {
		return announcement;
	}

	public void setAnnouncement(TbAnnouncement announcement) {
		this.announcement = announcement;
	}

	public List<TbAnnouncement> getList() {
		return list;
	}

	public void setList(List<TbAnnouncement> list) {
		this.list = list;
	}

	public Integer getAnnouncementId() {
		return announcementId;
	}

	public void setAnnouncementId(Integer announcementId) {
		this.announcementId = announcementId;
	}
	

}
